<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MQ限流机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            border-color: rgba(102, 126, 234, 0.3);
        }
        
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #667eea, transparent);
            margin: 3rem 0;
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 0.75rem 1.5rem;
            font-size: 0.875rem;
            color: #888;
            border-bottom: 1px solid #444;
        }
        
        pre {
            margin: 0;
            padding: 1.5rem;
            overflow-x: auto;
            color: #d4d4d4;
            font-family: 'Consolas', 'Monaco', monospace;
            line-height: 1.6;
        }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
            border-left: 4px solid #667eea;
            padding: 1.5rem;
            margin: 2rem 0;
            border-radius: 8px;
        }
        
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            margin: 2rem 0;
        }
        
        .strategy-card {
            background: white;
            border-radius: 16px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }
        
        .strategy-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
        }
        
        .strategy-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .icon-wrapper {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1.5rem;
            margin-bottom: 1rem;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <div class="floating-icon inline-block mb-6">
                <i class="fas fa-shield-alt text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6">MQ限流机制深度解析</h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                探索消息队列中的流量控制艺术，构建高可用分布式系统的关键技术
            </p>
        </div>
    </section>

    <!-- Introduction Section -->
    <section class="py-16 px-6">
        <div class="max-w-4xl mx-auto">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12">
                <p class="text-lg leading-relaxed text-gray-700 mb-6">
                    <span class="drop-cap">在</span>当今互联网时代，系统性能和稳定性是企业成功的关键因素之一。随着用户数量的增加和数据流量的增长，如何在高并发场景下保持系统的稳定性和响应速度成为了一个重要的挑战。在这样的背景下，限流机制应运而生，成为了保证系统稳定性和提升用户体验的利器。
                </p>
                <p class="text-lg leading-relaxed text-gray-700">
                    消息队列（MQ）作为分布式系统中常用的组件，不仅用于解耦、异步处理和削峰填谷，还扮演着实现限流的角色。通过对生产者和消费者的流量控制，MQ可以帮助我们在流量高峰期稳定系统性能，从而提高系统的可用性和可靠性。
                </p>
            </div>
        </div>
    </section>

    <!-- Rate Limiting Strategies -->
    <section class="py-16 px-6 bg-gray-50">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-4xl font-bold text-center mb-12 gradient-text">限流策略</h2>
            
            <div class="highlight-box mb-12">
                <h3 class="text-2xl font-semibold mb-4 text-gray-800">
                    <i class="fas fa-lightbulb text-yellow-500 mr-2"></i>限流策略的核心价值
                </h3>
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="flex items-start">
                        <i class="fas fa-shield-alt text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800">保护后端服务</h4>
                            <p class="text-gray-600">通过限制请求数量，防止后端服务因过载而崩溃</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-smile text-green-600 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800">提升用户体验</h4>
                            <p class="text-gray-600">通过平滑流量，提高系统的响应速度和稳定性</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-chart-line text-blue-600 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800">优化资源使用</h4>
                            <p class="text-gray-600">确保资源得到合理分配，避免不必要的资源浪费</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-layer-group text-orange-600 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800">业务优先级管理</h4>
                            <p class="text-gray-600">通过限流策略，能够为不同的业务赋予不同的优先级</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Strategy Cards -->
            <div class="grid md:grid-cols-2 gap-8">
                <div class="strategy-card">
                    <div class="icon-wrapper">
                        <i class="fas fa-window-maximize"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">固定窗口限流</h3>
                    <p class="text-gray-600 mb-4">
                        将时间划分为固定的窗口，在每个窗口内限制请求数量。实现简单，适用于请求流量相对均匀的场景。
                    </p>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-check-circle text-green-500 mr-2"></i>
                        <span>实现简单</span>
                        <i class="fas fa-times-circle text-red-500 ml-4 mr-2"></i>
                        <span>可能出现惊群效应</span>
                    </div>
                </div>

                <div class="strategy-card">
                    <div class="icon-wrapper">
                        <i class="fas fa-sliders-h"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">滑动窗口限流</h3>
                    <p class="text-gray-600 mb-4">
                        通过将大窗口划分为多个小窗口，记录每个小窗口内的请求数量，更精确地控制请求速率。
                    </p>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-check-circle text-green-500 mr-2"></i>
                        <span>更平滑的流量控制</span>
                        <i class="fas fa-times-circle text-red-500 ml-4 mr-2"></i>
                        <span>实现复杂度较高</span>
                    </div>
                </div>

                <div class="strategy-card">
                    <div class="icon-wrapper">
                        <i class="fas fa-coins"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">令牌桶算法</h3>
                    <p class="text-gray-600 mb-4">
                        定时向桶中放入令牌，只有获取到令牌的请求才能通过。支持突发流量，灵活性高。
                    </p>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-check-circle text-green-500 mr-2"></i>
                        <span>支持突发流量</span>
                        <i class="fas fa-times-circle text-red-500 ml-4 mr-2"></i>
                        <span>实现复杂度较高</span>
                    </div>
                </div>

                <div class="strategy-card">
                    <div class="icon-wrapper">
                        <i class="fas fa-tint"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">漏桶算法</h3>
                    <p class="text-gray-600 mb-4">
                        通过固定速率的方式漏出数据，保证请求的处理速率不会超过设定的最大值。
                    </p>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-check-circle text-green-500 mr-2"></i>
                        <span>平滑请求流量</span>
                        <i class="fas fa-times-circle text-red-500 ml-4 mr-2"></i>
                        <span>不支持突发流量</span>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Code Examples Section -->
    <section class="py-16 px-6">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-4xl font-bold text-center mb-12 gradient-text">实现示例</h2>
            
            <div class="space-y-8">
                <!-- Token Bucket Example -->
                <div class="code-block">
                    <div class="code-header">
                        <i class="fas fa-code mr-2"></i>令牌桶算法实现 - Java